<template>
    <div>
      <el-dialog title="员工信息" :visible.sync="isShow" @close="closeDialog">
                  <el-form :model="empForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="员工姓名" prop="ename">
              <el-input v-model="empForm.ename" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
          <el-radio-group v-model="empForm.sex" >
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
          <el-form-item label="学校">
              <el-input v-model="empForm.college" placeholder="请输入毕业学校"></el-input>
          </el-form-item>
          <el-form-item label="出生日期" prop="birthday">
                  <el-date-picker type="date" placeholder="请选择出生日期" v-model="empForm.birthday" style="width: 100%;" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item label="入职日期" prop="hiredate">
                  <el-date-picker type="date" placeholder="请选择入职日期" v-model="empForm.hiredate" style="width: 100%;" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item>
              <el-button type="primary" @click="submitForm">确定</el-button>
              <el-button @click="closeDialog">取消</el-button>
          </el-form-item>
          </el-form>
  
  
  
      </el-dialog>
    </div>
  </template>
  
  <script>
  export default {
      name:'EmpInfo',
      props:{
          isShow:Boolean,
          deptData:Array,
          empInfo:Object
      },
      data(){
            const validateHireDate = (rule, value, callback) => {
                if (!value) {
                    callback(new Error('请选择入职日期'));
                    return;
                }
                const hireDate = new Date(value);
                const today = new Date();
                hireDate.setHours(0, 0, 0, 0); 
                
                if (hireDate > today) {
                    callback(new Error('入职日期不能晚于今天'));
                } else {
                    callback();
                }
             };

            const validateAge = (rule, value, callback) => {
                if (!value) {
                    callback(new Error('请选择出生日期'));
                    return;
                }
                if (!this.empForm.hiredate) {
                    callback(); // 如果没有入职日期，先不验证
                    return;
                }
                
                const birthDate = new Date(value);
                const hireDate = new Date(this.empForm.hiredate);
                const ageDiff = hireDate.getFullYear() - birthDate.getFullYear();
                
                if (ageDiff < 20) {
                    callback(new Error('入职时年龄必须满20岁'));
                } else {
                    callback();
                }
            };
          return {
              deptList:[],
              empForm:{
                  empno:0,
                  ename:'',
                  sex:0,
                  job:'',
                  hiredate:'',
                  deptno:null,
                  isdelete:0,
                  birthday:'',
                  college:'',
                  userid:''
  
              },
              
              rules: {
            ename: [
              { required: true, message: '请输入员工姓名', trigger: 'blur' }
            ],
            college: [
              { required: true, message: '请输入毕业学校', trigger: 'blur' }
            ],
            hiredate: [
              { required: true, validator: validateHireDate, trigger: 'change' }
            ],
            birthday: [
              { required: true, validator: validateAge, trigger: 'change' }
            ]
           
          }
          }
      }
      ,
          methods:{
              closeDialog(){
                  this.empForm = {
                      empno:0,
                  ename:'',
                  sex:0,
                  job:'',
                  hiredate:'',
                  birthday:'',
                  college:'',
                  deptno:null,
                  isdelete:0,
                  userid:''
                  }
                  this.$refs['ruleForm'].resetFields();
                  this.$emit('closeDialog')
  
              },
              submitForm(){
                  this.$refs['ruleForm'].validate((valid) => {
                      if (valid) {
                          let url = '/addEmp'
                          if(this.empForm.empno != 0){
                              url = '/updateEmp'
                          }
                          this.$http.post(url,this.empForm).then(res => {
                              if(res.data === 'success'){
                                  this.$emit('empSave',res.data)
                              } else {
                                  this.$message.error("服务器错误")
                              }
  
                          })
                          
                         
                      } else {
                          console.log('error submit!!');
                          return false;
                      }
          });
                  
              }
          },
          watch:{
              
              empInfo:{
                  handler(val){

                      this.empForm = {...val}
                  }
              }
          }
  
  }
  </script>
  
  <style>
  
  </style>